package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.AccidentMapper;
import cn.wolfcode.crm.mapper.CarinfoMapper;
import cn.wolfcode.crm.mapper.ClaimcountMapper;
import cn.wolfcode.crm.mapper.CompensationInfoMapper;
import cn.wolfcode.crm.query.CarInfoQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICarInfoService;
import cn.wolfcode.crm.service.ICompensationInfoService;
import org.omg.CORBA.portable.IDLEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CompensationInfoServiceImpl implements ICompensationInfoService {

    @Autowired
    private CompensationInfoMapper compensationInfoMapper;

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private ClaimcountMapper claimcountMapper;



    @Override
    public int deleteByPrimaryKey(Long id) {
        return compensationInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(CompensationInfo record) {

        //在对明细表进行保存前要确定在表中是否已经存在此参数,如果存在就删除,不存在就在保存操作,确定唯一
        Long accidentId = record.getAccident().getId();
        CompensationInfo info = compensationInfoMapper.selectByaccidentId(accidentId);
        if(info ==null){
            return compensationInfoMapper.insert(record);
        }else{
            compensationInfoMapper.deleteByaccidentId(accidentId);
            return compensationInfoMapper.insert(record);
        }

    }

    @Override
    public CompensationInfo selectByPrimaryKey(Long id) {
        return compensationInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CompensationInfo> selectAll() {
        return compensationInfoMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(CompensationInfo record) {
        return compensationInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public CompensationInfo selectByaccidentId(Long accidentId) {
        return compensationInfoMapper.selectByaccidentId(accidentId);
    }

    @Override
    public PageResult query(QueryObject qo) {
        //1:查总条数
        int total = compensationInfoMapper.queryForCount(qo);

        if(total == 0){
            return new PageResult();
        }

        //2:查分页数据
        List<CompensationInfo> rows = compensationInfoMapper.queryForList(qo);

        return new PageResult(rows,total);
    }

    //查询出claimcount所需要的数据
    @Override
    public Map<Object,Object> selectClaimcountBypaymentid(Long id) {
        return compensationInfoMapper.selectClaimcountBypaymentid(id);
    }


    //保存到对象中
    @Override
    public void saveclaimcount(Long id) {
        Map<Object,Object> map = this.selectClaimcountBypaymentid(id);
        if (!map.isEmpty()){
            BigDecimal amount = (BigDecimal) map.get("amount");
            Claimcount claimcount = new Claimcount();
            claimcount.setAmount(amount);
            claimcount.setClientId((Long) map.get("accident_id"));
            claimcount.setAgencyId((Long) map.get("pay_agencyInfo_id"));
            claimcount.setBilldate(new Date());
            claimcountMapper.insert(claimcount);
        }
    }
}
